//
//  ECLoginViewController.m
//  ECERP.CRM
//
//  Created by Mac on 2018/10/18.
//  Copyright © 2018年 Mac. All rights reserved.
//

#import "ECLoginViewController.h"
#import "ECTextFieldView.h"
#import "ECIPSettingViewController.h"
#import "GLHTTPTool.h"
#import "GLBaseHTTPTool.h"
#import "GLHTTPParam.h"

@interface ECLoginViewController ()

@property (nonatomic,strong)ECTextFieldView *accountTf;
@property (nonatomic,strong)ECTextFieldView *passcodeTf;
@property (nonatomic,strong)ECTextFieldView *companyTf;
@property (nonatomic,strong)UIButton *loginBtn;
@property (nonatomic,strong)UIButton *freeBtn;
@end
extern NSString * Access_token;
extern NSString * merchantId;
@implementation ECLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title=@"登录";
    [self setupUI];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    CGRect viewBounds = self.view.bounds;
    self.freeBtn.frame=CGRectMake(self.view.width/2-40, viewBounds.size.height-80, 80, 40);
}

- (void)setupUI
{
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"Login_install_icon"] forState:UIControlStateNormal];
    [btn setTitle:@" 设置" forState:UIControlStateNormal];
    btn.frame=CGRectMake(0, 0, 44, 18);
    btn.titleLabel.font=ECFONT(18* PX_RELATIVE375);
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:btn];
    [btn addTarget:self action:@selector(installClickAction) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.accountTf];
    [self.view addSubview:self.passcodeTf];
    self.passcodeTf.tf.secureTextEntry=YES;
    [self.view addSubview:self.companyTf];
    [self.view addSubview:self.loginBtn];
    [self.view addSubview:self.freeBtn];
    
    self.companyTf.tf.userInteractionEnabled=NO;
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        [self installClickAction];
    }];
    
    [self.companyTf addGestureRecognizer:tap];
    
}

-(void)installClickAction
{
    ECIPSettingViewController *ipVC=[[ECIPSettingViewController alloc]init];
    [self.navigationController pushViewController:ipVC animated:YES];
}

- (ECTextFieldView *)accountTf
{
    if (!_accountTf) {
        _accountTf=[[ECTextFieldView alloc]initWithFrame:CGRectMake(35, 140, self.view.width-70, 48) iconString:@"Login_Accounts_icon" placeholder:@"请输入账号"];
    }
    return _accountTf;
}

- (ECTextFieldView *)passcodeTf
{
    if (!_passcodeTf) {
        _passcodeTf=[[ECTextFieldView alloc]initWithFrame:CGRectMake(35, self.accountTf.bottom, self.accountTf.width, self.accountTf.height) iconString:@"Login_password_icon" placeholder:@"请输入密码"];
    }
    return _passcodeTf;
}

- (ECTextFieldView *)companyTf
{
    if (!_companyTf) {
        _companyTf=[[ECTextFieldView alloc]initWithFrame:CGRectMake(35, self.passcodeTf.bottom, self.accountTf.width, self.accountTf.height) iconString:@"Login_company_icon" placeholder:@"请选择公司"];
    }
    return _companyTf;
}

- (UIButton *)loginBtn
{
    if (!_loginBtn) {
        _loginBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        _loginBtn.frame=CGRectMake(35, _companyTf.bottom+50, self.view.width-70, 44);
        _loginBtn.layer.cornerRadius=12.0;
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        _loginBtn.backgroundColor=ECCOLOR_NAVIBACK;
        [_loginBtn addTarget:self action:@selector(loginClickAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginBtn;
}

-(void)loginClickAction
{
    _accountTf.tf.text=@"yudongsheng33991010";
    _passcodeTf.tf.text=@"a987654";
    if (_accountTf.tf.text.length==0) {
        [MBProgressHUD showText:@"请输入账号" toView:self.view];
        return;
    }
    if (_passcodeTf.tf.text.length==0) {
        [MBProgressHUD showText:@"请输入密码" toView:self.view];
        return;
    }
    NSDictionary *dict=@{
                         @"username": _accountTf.tf.text,
                         @"password": _passcodeTf.tf.text,
                         @"grant_type": @"string",
                         @"userType": @"0"
                         };
    
    [GLHTTPTool postWithURL:Api_login params:dict success:^(id json) {
        if ([[json valueForKey:@"code"]intValue]==0) {
            [MBProgressHUD showSuccess:@"登录成功"];
            NSString * access_token = json[@"data"][@"access_token"];
            Access_token = access_token;
            NSString * access_merchantId = json[@"data"][@"MerchantId"];
            merchantId = access_merchantId;
            if (Access_token) {
                id  mainTabVC = [NSClassFromString(@"ECMainTabBarViewController") new];
                [UIApplication sharedApplication].keyWindow.rootViewController = mainTabVC;
            }
        }else{
            [MBProgressHUD showError:[json valueForKey:@"msg"]];
        }
    } failure:^(NSError *error) {

    }];
}

- (UIButton *)freeBtn
{
    if (!_freeBtn) {
        _freeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        _freeBtn.frame=CGRectMake(self.view.width/2-40, ECSCREEN_HEIGHT-80-44, 80, 40);
        [_freeBtn setTitle:@"免费试用" forState:UIControlStateNormal];
        [_freeBtn setTitleColor:ECCOLOR(0x29cc29) forState:UIControlStateNormal];
    }
    return _freeBtn;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
